s="  foo bar"
def countSegments(s):
    if s=="":
        return 0
    count=0
    index=1
    for i in range(len(s)):
        if s[i]!=" ":
            count=1
            index=i
            break

    if count==0:
        return count
    s_pointer=index
    while s_pointer<len(s):
        if s_pointer<len(s)-1 and s[s_pointer]==" " and s[s_pointer+1]!=" ":
            count+=1
        s_pointer+=1
    print(count)
countSegments(s)